package thrift;

import com.winwill.thrift.HelloWordService;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

/**
 * 多服务类测试
 */
public class MulityServiceServer {
    public static void main(String[] args) {
        try {
            TServerSocket serverTransport = new TServerSocket(9090);
            TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
            //创建多个服务Processor
            TestService.Processor<TestService.Iface> processorTest = new TestService.Processor<>(new TestServiceImpl());
           HelloWordService.Processor<HelloWordService.Iface> processorHello = new HelloWordService.Processor<>(new HelloWordServiceImpl());

            //将服务注册到TMultiplexedProcessor中
            TMultiplexedProcessor processors = new TMultiplexedProcessor();
            processors.registerProcessor("testService", processorTest);
            processors.registerProcessor("helloService", processorHello);

            TThreadPoolServer.Args serArgs = new TThreadPoolServer.Args(serverTransport)
                    .protocolFactory(protocolFactory)
                    .processor(processors)
                    .minWorkerThreads(5)
                    .maxWorkerThreads(5);
            TServer server = new TThreadPoolServer(serArgs);
            System.out.println("开启thrift服务器，监听端口：9090");
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }
}
